package schoolsys.controller;



import schoolsys.entity.Student;
import schoolsys.service.StudentService;
import schoolsys.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet(name = "updateStuServlet", value = "/updateStuServlet")
public class updateStuServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //先处理中文乱码
            request.setCharacterEncoding("utf-8");
            response.setCharacterEncoding("utf-8");

            String studentNo=request.getParameter("stuNo");
            String stuName=request.getParameter("stuName");
            String loginPwd=request.getParameter("loginPwd");
            String sex=request.getParameter("sex");
            String gradeId=request.getParameter("gradeId");
            String phone=request.getParameter("phone");
            String address=request.getParameter("address");
            String bornDate=request.getParameter("bornDate");
            String email=request.getParameter("email");
            String identityCard=request.getParameter("identityCard");
            long stuNo=0;
            if(studentNo!=null&&!"".equals(studentNo)){
                stuNo= Long.parseLong(studentNo);
            }

            Student stu=new Student();
            stu.setStudentName(stuName);
            stu.setAddress(address);
            stu.setPhone(phone);
            stu.setEmail(email);
            try {
                Date date=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(bornDate);
                stu.setBornDate(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            stu.setStudentNo(stuNo);
            stu.setGradeId(Long.parseLong(gradeId));
            stu.setIdentityCard(identityCard);
            stu.setLoginPwd(loginPwd);
            stu.setSex(Long.parseLong(sex));
            //调用service删除方法
            StudentService studentService=new StudentServiceImpl();
            int n=studentService.updateStuByNo(stu);
            response.sendRedirect("/SelectStuListServlet?action=delete&n="+n);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
